<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>滚动的车轮</title>
  <style>
    canvas {
      border: 1px solid #000;
    }
  </style>
</head>

<body>
  <canvas width="1200" height="600"></canvas>
  <script>
    const canvas = document.querySelector("canvas");
    const ctx = canvas.getContext("2d");

    const img = new Image();
    let deg = 0;
    // x坐标初始位置
    let x = 200;
    // 图片宽高
    const pictureWH = 200;
    // 每次旋转的度数
    const stepDeg = 0.05;

    img.src = "./img/PEN20.png";

    img.onload = function () {
      // 动画过程
      setInterval(() => {
        x++;
        deg += stepDeg;
        ctx.clearRect(0, 0, 1200, 600);
        // 备份
        ctx.save();
        // 平移
        ctx.translate(x, 200);
        // 旋转
        ctx.rotate(deg);
        // TODO: 核心 绘制 怎么样让这个园围绕自己的圆心去走
        ctx.drawImage(img, -pictureWH / 2, -pictureWH / 2, pictureWH, pictureWH);

        // 恢复
        ctx.restore();
      }, 18);
    }


  </script>
</body>

</html>